public class BruteForceSolution {

    public static void main(String[] args) {
        System.out.println(longestOnes(new int[]{1,0,0,0}, 2));
    }

    public static int longestOnes(int[] nums, int k) {
        int len = 0;
        int n = nums.length;
        for (int i = 0; i < n; i++) {
            int count = 0;
            int j = i;
            int distance = 0;
            for (; j < n; j++) {
                if (nums[j] == 1) {
                    distance++;
                } else if (nums[j] == 0 && count < k) {
                    distance++;
                    count++;
                } else {
                    break;
                }
            }
            len = Math.max(len, distance);
        }
        return len;
    }
}
